package com.lcj.eduCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcj.commonutils.JwtUtils;
import com.lcj.commonutils.MD5;
import com.lcj.eduCenter.entity.UcenterMember;
import com.lcj.eduCenter.entity.vo.RegistVo;
import com.lcj.eduCenter.mapper.UcenterMemberMapper;
import com.lcj.eduCenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcj.servicebase.config.exceptionHandler.EduException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new EduException(20001,"登陆失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper =new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileCenter = baseMapper.selectOne(wrapper);
        if(mobileCenter == null){//没有手机号
            throw new EduException(20001,"登陆失败");
        }
        //判断密码是否正确
        if(!MD5.encrypt(password).equals(mobileCenter.getPassword())){
            throw new EduException(20001,"登陆失败");
        }
        //判断用户是否被禁用
        if(mobileCenter.getIsDisabled()){
            throw new EduException(20001,"登陆失败");
        }
        //登陆成功
        String token = JwtUtils.getJwtToken(mobileCenter.getId(),mobileCenter.getNickname());
        return token;
    }

    //注册
    @Override
    public void register(RegistVo registVo) {
        //获取 注册的数据
        String code = registVo.getCode();
        String mobile = registVo.getMobile();
        String nickname = registVo.getNickname();
        String password = registVo.getPassword();

        //非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password)){
            throw new EduException(20001,"注册失败");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new EduException(20001,"验证码错误");
        }
        //判断手机号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>=0){
            throw new EduException(20001,"账户已存在");
        }
        UcenterMember member =new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
    }

    //根据openid判断用户是否存在
    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

}
